home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 105_01.zip / FLOAT.C < prev    next >
Text File  |  1993-06-09  |  7KB  |  315 lines

  1.  
  2.  
  3. /*
  4.     Floating point package support routines
  5.  
  6.     Note the "fp" library function, available in DEFF2.CRL,
  7.     is used extensively by all the floating point number
  8.     crunching functions.
  9.  
  10.     (see FLOAT.DOC for details...)
  11.  
  12.     NEW FEATURE: a special "printf" function has been included
  13.              in this source file for use with floating point
  14.              operands, in addition to the normal types. The
  15.              printf presented here will take precedence over
  16.              the DEFF.CRL version when "float" is specified
  17.              on the CLINK command line at linkage time.
  18.              Note that the "fp" function, needed by most of
  19.              the functions in this file, resides in DEFF2.CRL
  20.              and will be automatically collected by CLINK.
  21.  
  22.     All functions here written by Bob Mathias, except printf and
  23.     _spr (written by Leor Zolman.)
  24. */
  25.  
  26. #include "bdscio.h"
  27.  
  28. #define NORM_CODE    0
  29. #define ADD_CODE    1
  30. #define SUB_CODE    2
  31. #define MULT_CODE    3
  32. #define DIV_CODE    4
  33. #define FTOA_CODE    5
  34.  
  35. fpcomp(op1,op2)
  36.     char *op1,*op2;
  37. {
  38.     char work[5];
  39.     fpsub(work,op1,op2);
  40.     if (work[3] > 127) return (-1);
  41.     if (work[0]+work[1]+work[2]+work[3]) return (1);
  42.     return (0);
  43. }
  44.  
  45. fpnorm(op1) char *op1;
  46. {    fp(NORM_CODE,op1,op1);return(op1);}
  47.  
  48. fpadd(result,op1,op2)
  49.     char *result,*op1,*op2;
  50. {    fp(ADD_CODE,result,op1,op2);return(result);}
  51.  
  52. fpsub(result,op2,op1)
  53.     char *result,*op1,*op2;
  54.     {fp(SUB_CODE,result,op1,op2);return(result);}
  55.  
  56. fpmult(result,op1,op2)
  57.     char *result,*op1,*op2;
  58. {    fp(MULT_CODE,result,op1,op2);return(result);}
  59.  
  60. fpdiv(result,op1,op2)
  61.     char *result,*op1,*op2;
  62. {    fp(DIV_CODE,result,op1,op2);return(result);}
  63.  
  64. atof(fpno,s)
  65.     char fpno[5],*s;
  66. {
  67.     char *fpnorm(),work[5],ZERO[5],FP_10[5];
  68.     int sign_boolean,power;
  69.  
  70.     initb(FP_10,"0,0,0,80,4");
  71.     setmem(fpno,5,0);
  72.     sign_boolean=power=0;
  73.  
  74.     while (*s==' ' || *s=='\t') ++s;
  75.     if (*s=='-'){sign_boolean=1;++s;}
  76.     for (;isdigit(*s);++s){
  77.         fpmult(fpno,fpno,FP_10);
  78.         work[0]=*s-'0';
  79.         work[1]=work[2]=work[3]=0;work[4]=31;
  80.         fpadd(fpno,fpno,fpnorm(work));
  81.     }
  82.     if (*s=='.'){
  83.         ++s;
  84.         for (;isdigit(*s);--power,++s){
  85.             fpmult(fpno,fpno,FP_10);
  86.             work[0]=*s-'0';
  87.             work[1]=work[2]=work[3]=0;work[4]=31;
  88.             fpadd(fpno,fpno,fpnorm(work));
  89.         }
  90.     }
  91.     if (toupper(*s) == 'E') {++s; power += atoi(s); }
  92.     if (power>0)
  93.         for (;power!=0;--power) fpmult(fpno,fpno,FP_10);
  94.     else
  95.     if (power<0)
  96.         for (;power!=0;++power) fpdiv(fpno,fpno,FP_10);
  97.     if (sign_boolean){
  98.         setmem(ZERO,5,0);
  99.         fpsub(fpno,ZERO,fpno);
  100.     }
  101.     return(fpno);
  102. }
  103. ftoa(result,op1)
  104.     char *result,*op1;
  105. {    fp(FTOA_CODE,result,op1);return(result);}
  106.  
  107. itof(op1,n)
  108. char *op1;
  109. int n;
  110. {
  111.     char temp[20];
  112.     return atof(op1, itoa(temp,n));
  113. }
  114.  
  115. itoa(str,n)
  116. char *str;
  117. {
  118.     char *sptr;
  119.     sptr = str;
  120.     if (n<0) { *sptr++ = '-'; n = -n; }
  121.     _uspr(&sptr, n, 10);
  122.     *sptr = '\0';
  123.     return str;
  124. }
  125.  
  126.  
  127. /*
  128.     The short "printf" function given here is exactly the
  129.     same as the one in the library, but it needs to be placed
  130.     here so that the special "_spr" is used instead of the
  131.     normal one in DEFF.CRL. The way the linker works is that
  132.     a function is not linked in UNTIL IT IS REFERENCED...so
  133.     if the definition of "printf" were not placed here in this
  134.     file, "_spr" would not be referenced at all
  135.     until the "printf" from DEFF.CRL got yanked in, at which time
  136.     "_spr" would ALSO be taken from DEFF.CRL and cause the
  137.     floating point "_spr" options to not be recognized.
  138.  
  139.     In other words, if "printf" were not given explicitly here,
  140.     the WRONG _spr would end up being used.
  141. */
  142.  
  143.  
  144. printf(format)
  145. char *format;
  146. {
  147.     char line[MAXLINE];
  148.     _spr(line,&format);    /* use "_spr" to form the output */
  149.     puts(line);        /* and print out the line     */
  150. }
  151.  
  152.  
  153. /*
  154.     This is the special formatting function, which supports the
  155.     "e" and "f" conversions as well as the normal "d", "s", etc.
  156.     When using "e" or "f" format, the corresponding argument in
  157.     the argument list should be a pointer to one of the five-byte
  158.     strings used as floating point numbers by the floating point
  159.     functions. Note that you don't need to ever use the "ftoa"
  160.     function when using this special printf/sprintf combination;
  161.     to achieve the same result as ftoa, a simple "%e" format
  162.     conversion will do the trick. "%f" is used to eliminate the
  163.     scientific notation and set the precision. The only [known]
  164.     difference between the "e" and "f" conversions as used here
  165.     and the ones described in the Kernighan & Ritchie book is that
  166.     ROUNDING does not take place in this version...e.g., printing
  167.     a floating point number which happens to equal exactly 3.999
  168.     using a "%5.2f" format conversion will produce " 3.99" instead
  169.     of " 4.00".
  170. */
  171.  
  172.  
  173. _spr(line,fmt)
  174. char *line, **fmt;
  175. {
  176.     char _uspr(), c, base, *sptr, *format;
  177.     char wbuf[80], *wptr, pf, ljflag;
  178.     int width, precision, exp, *args;
  179.  
  180.     format = *fmt++;    /* fmt first points to the format string */
  181.     args = fmt;        /* now fmt points to the first arg value */
  182.     while (c = *format++)
  183.       if (c == '%') {
  184.         wptr = wbuf;
  185.         precision = 6;
  186.         ljflag = pf = 0;
  187.  
  188.         if (*format == '-') {
  189.             format++;
  190.             ljflag++;
  191.          }
  192.  
  193.         if ( !(width = _gv2(&format))) width++;
  194.  
  195.         if ((c = *format++) == '.') {
  196.             precision = _gv2(&format);
  197.             pf++;
  198.             c = *format++;
  199.          }
  200.  
  201.         switch(toupper(c)) {
  202.         case 'E':  if (precision>7) precision = 7;
  203.                ftoa(wbuf,*args++);
  204.                strcpy(wbuf+precision+3, wbuf+10);
  205.                width -= strlen(wbuf);
  206.                goto pad2;
  207.  
  208.         case 'F':  ftoa(&wbuf[60],*args++);
  209.                sptr = &wbuf[60];
  210.                while ( *sptr++ != 'E')
  211.                 ;
  212.                exp = atoi(sptr);
  213.                sptr = &wbuf[60];
  214.                if (*sptr == ' ') sptr++;
  215.                if (*sptr == '-') {
  216.                 *wptr++ = '-';
  217.                 sptr++;
  218.                 width--;
  219.                 }
  220.                sptr += 2;
  221.  
  222.                if (exp < 1) {
  223.                 *wptr++ = '0';
  224.                 width--;
  225.                 }
  226.  
  227.                pf = 7;
  228.                while (exp > 0 && pf) {
  229.                 *wptr++ = *sptr++;
  230.                 pf--;
  231.                 exp--;
  232.                 width--;
  233.                 }
  234.  
  235.                while (exp > 0) {
  236.                 *wptr++ = '0';
  237.                 exp--;
  238.                 width--;
  239.                 }
  240.  
  241.                *wptr++ = '.';
  242.                width--;
  243.  
  244.                while (exp < 0 && precision) {
  245.                 *wptr++ = '0';
  246.                 exp++;
  247.                 precision--;
  248.                 width--;
  249.                 }
  250.  
  251.                while (precision && pf) {
  252.                 *wptr++ = *sptr++;
  253.                 pf--;
  254.                 precision--;
  255.                 width--;
  256.                 }
  257.  
  258.                while (precision>0) {
  259.                 *wptr++ = '0';
  260.                 precision--;
  261.                 width--;
  262.                 }
  263.  
  264.                goto pad;
  265.  
  266.  
  267.         case 'D':  if (*args < 0) {
  268.                 *wptr++ = '-';
  269.                 *args = -*args;
  270.                 width--;
  271.                 }
  272.         case 'U':  base = 10; goto val;
  273.  
  274.         case 'X':  base = 16; goto val;
  275.  
  276.         case 'O':  base = 8;
  277.  
  278.              val:  width -= _uspr(&wptr,*args++,base);
  279.                goto pad;
  280.  
  281.         case 'C':  *wptr++ = *args++;
  282.                width--;
  283.                goto pad;
  284.  
  285.         case 'S':  if (!pf) precision = 200;
  286.                sptr = *args++;
  287.                while (*sptr && precision) {
  288.                 *wptr++ = *sptr++;
  289.                 precision--;
  290.                 width--;
  291.                 }
  292.  
  293.              pad:  *wptr = '\0';
  294.              pad2: wptr = wbuf;
  295.                if (!ljflag)
  296.                 while (width-- > 0)
  297.                     *line++ = ' ';
  298.  
  299.                while (*line = *wptr++)
  300.                 line++;
  301.  
  302.                if (ljflag)
  303.                 while (width-- > 0)
  304.                     *line++ = ' ';
  305.                break;
  306.  
  307.          default:  *line++ = c;
  308.  
  309.          }
  310.       }
  311.       else *line++ = c;
  312.  
  313.     *line = '\0';
  314. }
  315.